﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;
using TMDT.Web.UI.Commons;

namespace TMDT.Web.UI.Controls
{
    public partial class ChiTietThuocTinhControl : System.Web.UI.UserControl
    {
        private static Guid LoaiSanPham { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }

        #region Methods

        private void LoadData()
        {
            LoadDropDownList();
            LoadRepeater();
        }

        private void LoadDropDownList()
        {
            ddlLoaiSanPham.LoadDataSource(new LoaiSanPhamManager().GetLoaiSanPham(), "TenLoaiSanPham", "IDLoaiSanPham");

            if (LoaiSanPham == Guid.Empty)
            {
                LoaiSanPham = new Guid(ddlLoaiSanPham.SelectedValue);
            }
            else
            {
                ddlLoaiSanPham.SelectedValue = LoaiSanPham.ToString();
            }
        }

        private void LoadRepeater()
        {
            rptChiTietThuocTinh.DataSource = new ThuocTinhManager().GetThuocTinh();

            rptChiTietThuocTinh.DataBind();
        }

        #endregion

       
       

        #region Event

        protected void ibnAddRemove_Click(object sender, ImageClickEventArgs e)
        {
            var service = new ChiTietThuocTinhManager();

            var idThuocTinh = ((ImageButton)sender).CommandArgument;

            var entity = new ChiTietThuocTinh
            {
                IDLoaiSanPham = LoaiSanPham,
                IDThuocTinh = new Guid(idThuocTinh)
            };
            if( service.IsChiTietThuocTinhTonTai(entity))
            {
                var a = service.Remove(entity);
            }
            else
            {
                var b = service.Add(entity);
            }

            LoadRepeater();
        }

        protected void ibnRemove_Click(object sender, ImageClickEventArgs e)
        {
            foreach (var tt in rptChiTietThuocTinh.Items)
            {
                
            }
            //var service = new ChiTietThuocTinhManager();

            //var idThuocTinh = ((ImageButton)sender).CommandArgument;

            //var entity = service.GetChiTietThuocTinh(new LoaiSanPhamManager().GetLoaiSanPham(LoaiSanPham), new ThuocTinhManager().GetThuocTinh(idThuocTinh));
            //service.Remove(entity);

            //LoadRepeater();
        }

        protected void rptChiTietThuocTinh_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var thuocTinh = e.Item.DataItem as ThuocTinh;
            var loaiSanPham = new LoaiSanPhamManager().GetLoaiSanPham(LoaiSanPham);

            var ibnAddRemove = e.Item.FindControl("ibnAddRemove") as ImageButton;
            var lblThuocTinh = e.Item.FindControl("lblThuocTinh") as Label;

            lblThuocTinh.Text = thuocTinh.TenThuocTinh;
            var entity = new ChiTietThuocTinh
                             {
                                 IDLoaiSanPham = loaiSanPham.IDLoaiSanPham,
                                 IDThuocTinh = thuocTinh.IDThuocTinh
                             };

            var svChiTietThuocTinh= new ChiTietThuocTinhManager();

            if (svChiTietThuocTinh.IsChiTietThuocTinhTonTai(entity))
            {
                ibnAddRemove.ImageUrl = "../Images/delete.png";
            }
            else
            {
                ibnAddRemove.ImageUrl = "../Images/add.png";
            }

        }

        protected void ddlLoaiSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoaiSanPham = new Guid(ddlLoaiSanPham.SelectedValue);

            LoadRepeater();
        }

        #endregion


    }
}